package com.hzncc.flowable_diboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.service.impl.BaseServiceImpl;
import com.diboot.core.vo.JsonResult;
import com.hzncc.flowable_diboot.entity.OppoFlowTemplate;
import com.hzncc.flowable_diboot.entity.OppoFlowTemplateItem;
import com.hzncc.flowable_diboot.mapper.OppoFlowTemplateMapper;
import com.hzncc.flowable_diboot.service.OppoFlowTemplateItemService;
import com.hzncc.flowable_diboot.service.OppoFlowTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
* 商机流程模板 相关Service实现类
* @author MyName
* @version 1.0
* @date 2024-06-11
* Copyright © MyCorp
*/
@Slf4j
@Service
public class OppoFlowTemplateServiceImpl extends BaseServiceImpl<OppoFlowTemplateMapper, OppoFlowTemplate> implements OppoFlowTemplateService {

    @Autowired
    private OppoFlowTemplateItemService oppoFlowTemplateItemService;

    /**
     * 复制
     * @param id
     * @return
     */
    @Override
    public JsonResult<?> copy(String id) {
        OppoFlowTemplate oppoFlowTemplate = getEntity(id);
        if (null == oppoFlowTemplate) {
            return JsonResult.FAIL_EXCEPTION("模板不存在");
        }
        oppoFlowTemplate.setId(null);
        oppoFlowTemplate.setUpdateBy(null);
        oppoFlowTemplate.setUpdateTime(null);
        oppoFlowTemplate.setCreateBy(null);
        oppoFlowTemplate.setCreateTime(null);
        oppoFlowTemplate.setCode(oppoFlowTemplate.getCode()+"_copy");
        oppoFlowTemplate.setName(oppoFlowTemplate.getName() + "_copy");
        oppoFlowTemplate.setStatus(0);
        createEntity(oppoFlowTemplate);
        QueryWrapper<OppoFlowTemplateItem> qw = new QueryWrapper<>();
        qw.lambda().eq(OppoFlowTemplateItem::getTemplateId, id);
        List<OppoFlowTemplateItem> entityList = oppoFlowTemplateItemService.getEntityList(qw);
        entityList.forEach(item -> {
            item.setId(null);
            item.setUpdateBy(null);
            item.setUpdateTime(null);
            item.setCreateBy(null);
            item.setCreateBy(null);
            item.setTemplateId(oppoFlowTemplate.getId());
        });
        oppoFlowTemplateItemService.createEntities(entityList);
        return JsonResult.OK();
    }

}